package com.douqu.game.core.entity.world.map;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.entity.EntityObject;
import com.douqu.game.core.entity.battle.BattleTemp;
import com.douqu.game.core.factory.ConfigFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.WorldInfoFactory;
import com.douqu.game.core.protobuf.SGCommonProto;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-04-26 16:10
 */
public class SpriteBean extends EntityObject {

//    public volatile boolean startMatch = false;
    /**
     *  玩家流水号
     *　只有单人战场这个字段才有值
     */
    protected String masterPlayerIndex;

    /**
     * 超时删除，其它操作不允许
     */
    protected boolean isRemove;

    protected String objectIndex;
    /**
     * 当前状态
     */
    protected SGCommonProto.E_ARMY_STATUS status;

    /** 上一次状态变化时间 */
    protected long lastUpdateStatusTime;

    /**
     * 属于哪个组
     */
    protected SGCommonProto.E_ARMY_TEAM team;

    protected int camp;

    /**
     * 是否在城里
     */
    protected boolean isInCity;


    /**
     * 开始保护时间
     */
    protected long startProtectTime;

    protected BattleTemp battleTemp;

    protected PlayerBaseInfo baseInfo;

    public SpriteBean()
    {
        this.status = SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_FREE;
        this.lastUpdateStatusTime = DataFactory.currentTime;
        this.team = SGCommonProto.E_ARMY_TEAM.ARMY_TEAM_UNKNOWN;
        this.baseInfo = new PlayerBaseInfo();
    }


    public void loadFrom(ByteBuffer buffer)
    {
        objectIndex = buffer.readUTF();
    }

    public void writeTo(ByteBuffer buffer)
    {
        buffer.writeUTF(objectIndex);
    }


    public void sendWarn(String key, Object... params)
    {

    }

    public void sendMsg(int code, byte[] data)
    {

    }

    public void update(WorldMapUpdateResult updateResult)
    {

    }

    public void stop()
    {

    }


    public void updateBattleTemp(BattleTemp battleTemp)
    {
        if(battleTemp == null)
            return;

        this.battleTemp = battleTemp;
    }

    public void die()
    {
        if(battleTemp == null)
            return;

        battleTemp.die();
    }

    public boolean isDefender()
    {
        return team == SGCommonProto.E_ARMY_TEAM.ARMY_TEAM_DEF;
    }

    public boolean isAttacker()
    {
        return team == SGCommonProto.E_ARMY_TEAM.ARMY_TEAM_ATK;
    }

    public SGCommonProto.PlayerBaseInfo.Builder parseBaseInfo()
    {
        return baseInfo.parseBaseInfo();
    }

    public boolean isOverdueStatus()
    {
        return lastUpdateStatusTime > 0 && DataFactory.currentTime - lastUpdateStatusTime >= ConfigFactory.WORLD_MAP_STATUS_WAIT_TIME;
    }

    public boolean isOverdueStatus(int overdueTime)
    {
        return lastUpdateStatusTime > 0 && DataFactory.currentTime - lastUpdateStatusTime >= overdueTime;
    }

    /**
     * 是否在此城待的时间超过10秒
     * @return
     */
    public boolean isOverWait()
    {
        return status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_WAIT && DataFactory.currentTime - lastUpdateStatusTime >= ConfigFactory.WORLD_MAP_STATUS_WAIT_TIME && lastUpdateStatusTime > 0;
    }

    /**
     * 是否在队列里超过10秒没动作
     * @return
     */
    public boolean isOverMatch()
    {
        return status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_MATCH && DataFactory.currentTime - lastUpdateStatusTime >= ConfigFactory.WORLD_MAP_STATUS_WAIT_TIME && lastUpdateStatusTime > 0;
    }

    public boolean isCanBreak()
    {
        return status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_WAIT || status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_MATCH;
    }

    /**
     * 是否在保护期内
     * @return
     */
    public boolean isInProtectTime()
    {
        return startProtectTime > 0 && DataFactory.currentTime - startProtectTime < ConfigFactory.WORLD_MAP_BATTLE_WAIT_TIME;
    }

    public boolean isFree()
    {
        return status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_FREE;
    }

    public boolean isBattleOrPrepare()
    {
        return status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_PREPARE || status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_BATTLE;
    }

    public boolean isBattle()
    {
        return status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_BATTLE;
    }

    public boolean isPrepare()
    {
        return status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_PREPARE;
    }

    /**
     * 是否是路人(没进城的)
     * @return
     */
    public boolean isPasserby()
    {
        return status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_FREE || status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_MOVE || status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_WAIT;
    }

    public boolean isRecover()
    {
        return status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_RECOVER;
    }

    public boolean isDie()
    {
        return status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_DIE;
    }

    public boolean isMatch()
    {
        return status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_MATCH;
    }

    public boolean isWait()
    {
        return status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_WAIT;
    }

    public boolean isMove()
    {
        return status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_MOVE;
    }

//    public boolean isFreeOrMove()
//    {
//        return status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_FREE || status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_MOVE;
//    }
//
//    public boolean isCanMove()
//    {
//        return status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_FREE || status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_MOVE || status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_QUEUE || status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_WAIT;
//    }



    public SGCommonProto.E_ARMY_STATUS getStatus() {
        return status;
    }

    public void setStatus(SGCommonProto.E_ARMY_STATUS status)
    {
        this.status = status;
        this.lastUpdateStatusTime = DataFactory.currentTime;

        if(status != SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_MATCH)
            setStartProtectTime(0);
    }



    public int getCamp() {
        return camp;
    }

    public void setCamp(int camp) {
        this.camp = camp;
    }

    public void setObjectIndex(String objectIndex) {
        this.objectIndex = objectIndex;
    }

    public String getObjectIndex() {
        return objectIndex;
    }

    public int getCurCity() {
        return WorldInfoFactory.getInstance().getWorldMapData(masterPlayerIndex).getSpriteCurCity(objectIndex);
    }

    public void setCurCity(int curCity) {
        WorldInfoFactory.getInstance().getWorldMapData(masterPlayerIndex).setSpriteCurCity(objectIndex, curCity);
    }

    public SGCommonProto.E_ARMY_TEAM getTeam() {
        return team;
    }

    public void setTeam(SGCommonProto.E_ARMY_TEAM team) {
        this.team = team;
    }

    public long getLastUpdateStatusTime() {
        return lastUpdateStatusTime;
    }

    public void setLastUpdateStatusTime(long lastUpdateStatusTime) {
        this.lastUpdateStatusTime = lastUpdateStatusTime;
    }


    public boolean isInCity() {
        return isInCity;
    }

    public void setInCity(boolean isInCity) {
        this.isInCity = isInCity;
    }

    public String getName() {
        return baseInfo.getNickName();
    }

    public String getNameLv()
    {
        return baseInfo.getNickName() + "(Lv" + baseInfo.getLv() + ")";
    }

//    public void setName(String name) {
//        this.name = name;
//    }

    public long getStartProtectTime() {
        return startProtectTime;
    }

    public void setStartProtectTime(long startProtectTime) {
        this.startProtectTime = startProtectTime;
    }

    public boolean isRemove() {
        return isRemove;
    }

    public void setRemove(boolean isRemove) {
        this.isRemove = isRemove;
    }

    public PlayerBaseInfo getBaseInfo() {
        return baseInfo;
    }

    public BattleTemp getBattleTemp()
    {
        return battleTemp;
    }

    public String getMasterPlayerIndex() {
        return masterPlayerIndex;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SpriteBean)) return false;

        SpriteBean that = (SpriteBean) o;

        if (!objectIndex.equals(that.objectIndex)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return objectIndex == null ? 0 : objectIndex.hashCode();
    }

    @Override
    public String toString() {
        return "{" +
                "masterPlayerIndex='" + masterPlayerIndex + '\'' +
                ", objectIndex='" + objectIndex + '\'' +
                ", status=" + status +
                ", lastUpdateStatusTime=" + lastUpdateStatusTime +
                ", team=" + team +
                ", camp=" + camp +
                ", isInCity=" + isInCity +
//                ", battleTemp=" + battleTemp +
                ", baseInfo=" + baseInfo +
                "} ";
    }
}
